package com.guet.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.guet.DTO.ManagementPageDTO;
import com.guet.Entity.User;
import com.guet.VO.ManagementVO;
import com.guet.constant.UserConstant;
import com.guet.mapper.ManagementMapper;
import com.guet.result.PageResult;
import com.guet.service.ManagementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.List;

@Service
public class ManagementServiceImpl implements ManagementService {
    @Autowired
    private ManagementMapper managementMapper;

    /**
     * 分页查询学生信息
     *
     * @return
     */
    @Override
    public PageResult list(ManagementPageDTO managementPageDTO) {
        PageHelper.startPage(managementPageDTO.getCurrentPage(), managementPageDTO.getPageSize());
        Page<ManagementVO> page = managementMapper.list(managementPageDTO);
        Long total = page.getTotal();
        List<ManagementVO> list = page.getResult();
        return new PageResult(total, list);
    }

    /**
     * 修改学生账号状态
     *
     * @param id
     */
    @Override
    public void updateStatus(Integer id, String status) {
        if (status.equals("0"))
            status = UserConstant.USER_ENABLED;
        else if (status.equals("1"))
            status = UserConstant.USER_DISABLED;
        User user = User.builder().id(id).status(status).build();
        managementMapper.update(user);
    }

    /**
     * 重置学生账号密码
     *
     * @param id
     */
    @Override
    public void updatePassword(Integer id) {
        String password = DigestUtils.md5DigestAsHex(UserConstant.DEFAULT_PASSWORD.getBytes());
        User user = User.builder().id(id).password(password).build();
        managementMapper.update(user);
    }


}
